<?php

namespace App\Http\Controllers\Admin;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends BaseController
{

    //用户列表展示
    public function index(){
        $data = User::orderBy('id','asc')->paginate($this->pagesize);
        return view('admin.user.index',compact('data'));
    }
    //添加用户显示
    public function create(){
        return view('admin.user.create');
    }
    //添加用户操作
    public function store(Request $request){
       //对数据进行验证
//        $this->validate($request,[
//            'username' => 'required',
//            'truename' => 'required',
//            'password' => 'required',
//            'phone' => 'nullable|phone'
//        ],[
//            'phone.phone' => '手机号码不合法'
//        ]);

        //第二种数据验证的方式
        $this->validate($request,[
            //用户名验证唯一性
            'username' => 'required|unique:users,username',
            'truename' => 'required',
            //两次密码是否一致的验证
            'password' => 'required|confirmed',
            //自定义验证规则
            'phone' => 'required|phone'
        ]);
        $post = $request->except(['_token','password_confirmation']);
        //添加用户入库
        $userModel = User::create($post);
        //发送邮件
        //

        //跳转到列表页
        return redirect(route('admin.user.create'))->with('success','添加成功');
    }


    //删除用户
    public function del(int $id){
        //删除
        User::find($id)->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }

}
